﻿	// Copyright 2010 Edmundo Pavel Soriano Morales

    // This file is part of eltokenizer.

    // eltokenizer is free software: you can redistribute it and/or modify
    // it under the terms of the GNU General Public License as published by
    // the Free Software Foundation, either version 3 of the License, or
    // (at your option) any later version.

    // eltokenizer is distributed in the hope that it will be useful,
    // but WITHOUT ANY WARRANTY; without even the implied warranty of
    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    // GNU General Public License for more details.

    // You should have received a copy of the GNU General Public License
    // along with eltokenizer.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Tokenizer
{
    class ColeccionDocumentos
    {
        public List<Documento> coleccionDocumentos { get; set; }
        public int numeroDocumentos { get; set; }

        public ColeccionDocumentos(List<Documento> coleccionDocumentos)
        {
            this.coleccionDocumentos = coleccionDocumentos;
            this.numeroDocumentos = this.coleccionDocumentos.Count;
            CalculaTFIDFs();
            
        }
        public void CalculaTFIDFs()
        {
            Dictionary<string, int> tokenNumDocsAparece = new Dictionary<string, int>();
            int cuenta = 0;
            foreach (var documento in coleccionDocumentos)
                foreach (var token in documento.tiposEnDocumento)
                {
                    
                    if (tokenNumDocsAparece.TryGetValue(token.token, out cuenta))
                        tokenNumDocsAparece[token.token] += 1;
                    else
                        tokenNumDocsAparece[token.token] = 1;
                }

            foreach (var documento in coleccionDocumentos)
                foreach (var token in documento.tiposEnDocumento)
                {
                    token.numDocsAparece = tokenNumDocsAparece[token.token];
                    token.tfIdf = token.freqRelativa * (Math.Log((double)this.numeroDocumentos / (double)token.numDocsAparece));
                }
            
        }

        public void ImprimeInfo(int orden, string txtsdiferentes)
        {
            
            List<Token> todosLosTokens = new List<Token>();
            IOrderedEnumerable<Token> tokensOrdenados;
            if (txtsdiferentes == "juntos")
            {
                TextWriter tw = new StreamWriter("palabrasTodas.txt");
                foreach (var documento in coleccionDocumentos)
                    todosLosTokens.AddRange(documento.tiposEnDocumento);
                if (orden == 2)                
                    tokensOrdenados = todosLosTokens.OrderBy(t => t.token);
                
                else                
                    tokensOrdenados = todosLosTokens.OrderBy(t => t.tfIdf);
                    
                

                tokensOrdenados.ToList().ForEach(l=>tw.WriteLine(l.token + "\t\ttf-idf: " + l.tfIdf + "\tNúmero de docs donde aparece: " + l.numDocsAparece + " de " + this.numeroDocumentos.ToString() + 
                                            "\tFrecuencia Abs: " + l.freqAbsoluta + "\tFrecuencia Relativa: " + l.freqRelativa)) ;
                tw.Close();
                
            }
                    
        }


    }
}
